import json


# 1.dump和load
data = {
    "name": "Alice",
    "age": 30,
    "hobbies": ["reading", "hiking"],
    "is_student": False
}
#dump主要将Python数据转为JSON并写入文件
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

with open("data.json", "r", encoding="utf-8") as f:
    loaded_data = json.load(f)

print(loaded_data)


#2.write、read、seek、tell函数
#写入，读取、调整指针，获取指针位置
with open("anxin.txt","w+",encoding="utf-8") as f:
    f.write("安心最帅")
    print(f.tell())
    f.seek(0)
    print(f.read())


#3.writelines和readlines函数
#批量写入和读取
lines_to_write = [
    "第一行写入\n",
    "第二行写入\n",#换行符要手动写入
    "第三行写入"
]

with open("output.txt", "w", encoding="utf-8") as f:
    f.writelines(lines_to_write)

with open("output.txt", "r", encoding="utf-8") as f:
    lines = f.readlines()
    print("读取到的行列表：", lines)

for line in lines:
    print("处理后：", line.strip())#移除字符串首尾的指定字符











